$(function () {
    //初始化专业搜索条件
    $.initSubject({
        element:'subject-box',
        confirmBtn:false,
        removeBtn: false,
        style:false,
        notMultiple:true,
        classText:'form-control mb10',
        width:100,
        selectCallback:function(subjectIds){
            $("#subjectIds").val(subjectIds);
        }
    });

    //加载课程数据
    listCourse(1);
    $("body").on('click', '#btn1,#btn2', function () { //搜索课程
        listCourse(1);
    });
});

//加载课程数据
function listCourse(page) {
    var params = $("#search-form").serializeJSON();
    params = JSON.stringify(params);
    var obj = {
        url: baseloaction + '/admin/course/cou/ajaxCoursePracticeList',//请求URL
        contextId: 'course-list-context',//响应内容页面设置内容ID
        params: params,//请求参数
        pageNo: page,//页码
        pageCall: listCourse, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
        selectAllId: 'all-course', //全选框ID
        callback: function () {//数据加载完成回调
            lazyCourseImage();
            //加载课程ztree
            setTimeout(function(){
                loadCourseCatalog();
            },200)
        }
    };
    ajaxGetListData(obj);
}

    function loadCourseCatalog(){
    var dom = $('.ztree');
        if(dom.length>0){
            for(var i=0;i<dom.length;i++){
                var courseId =   $(dom[i]).data("id");
                var id = $(dom[i]).attr("id");
               var percent=  $(dom[i]).data("percent");
                var  courseName =  $(dom[i]).parents(".row").find(".txtOf").text();
                var  scorePercent =  $(dom[i]).parents(".row").find(".scorePercent").text();
                var  imgUrl =  $(dom[i]).parents(".row").find(".lazy").data("original");

                initCourseCatalog(courseId,id,percent,courseName,scorePercent,imgUrl)
            }
        }
    }


function initCourseCatalog(courseId,id,percent,courseName,scorePercent,imgUrl) {

        var settings = {
            async: {
                dataType: 'json',
                dataFilter: function (treeId, treeNode, response) {
                    if (response.success) {
                        var list = response.entity;
                        if(list!=null && list.length>0){
                            $.each(list,function (index,em) {

                                if(em.materialId==0&&em.nodeType==2){
                                    em.catalogName = em.catalogName+'(没有学习资源)';
                                }else if(em.materialTypeKey=="LIVE"&&em.nodeType==2){
                                    em.catalogName = "[直播] "+em.catalogName+' ['+em.lessonStartTime.replace(/-/g,"/")+' - '+em.lessonEndTime.replace(/-/g,"/")+']';
                                }else if(em.nodeType==2){
                                    var type ="";
                                    switch ( em.materialTypeKey) {
                                        case "VIDEO":
                                            type="视频";
                                            break;
                                        case "AUDIO":
                                            type="音频";
                                            break;
                                        case "ARTICLE":
                                            type="文章";
                                            break;

                                    }
                                    em.catalogName = "["+type+"] "+em.catalogName;
                                    if (em.questionIds!=null){
                                        em.catalogName ="[随堂练习] "+em.catalogName;
                                    }
                                }
                            });
                        }
                        return response.entity;
                    } else {
                        icModale(0, response.message);
                    }
                },
                enable: true,
                type: 'post',
                url: baseloaction + "/admin/course/catalog/findCourseCatalogPracticeByCourseId?courseId=" + courseId+"&scoreRate="+percent
            },
            check: {
                enable: false
            },
            data: {
                key: {
                    name: 'catalogName',
                    title: 'catalogName',
                    mId: 'mouldId'
                },
                simpleData: {
                    enable: true,
                    idKey: 'id',
                    pIdKey: 'parentId',
                    rootPId: null
                }
            },
            view: {
                addHoverDom: function (treeId, treeNode) {
                    var sObj = $("#" + treeNode.tId + "_span");
                    if (treeNode.questionIds!=null&&$("#practice_" + treeNode.tId).length<=0){
                       /* var text = '  练习人数：'+treeNode.practiceHeadcount;
                         text += '   通过人数：'+treeNode.practicePassNumber;
                        sObj.addClass("c-red");*/
                        var addStrTo = "<span style='margin-left: 10px;' id='practice_" + treeNode.tId + "'>【    <a style='color: #4387f8;margin-left: 5px;' id='practice_info_" + treeNode.tId + "'>查看详情 》</a>】</span>";
                        sObj.after(addStrTo);
                        var btn = $("#practice_info_" + treeNode.tId);
                        if (btn) btn.bind("click", function () {
                            sessionStorage.setItem("scorePercent",scorePercent);
                            sessionStorage.setItem("practiceHeadcount",treeNode.practiceHeadcount);
                            sessionStorage.setItem("practicePassNumber",treeNode.practicePassNumber);
                            sessionStorage.setItem("name",treeNode.catalogName);
                            sessionStorage.setItem("courseName",courseName);
                            sessionStorage.setItem("imgUrl",imgUrl);
                            sessionStorage.setItem("courseId",courseId);
                            sessionStorage.setItem("catalogId",treeNode.id);
                            sessionStorage.setItem("percent",percent);
                          window.location.href=  baseloaction+"/admin/course/cou/coursePracticeInfo";
                        });

                    }
                },
                removeHoverDom: function (treeId, treeNode) {//鼠标离开删除自定义按钮
                    $("#practice_" + treeNode.tId).unbind().remove();
                },
                fontCss: function (treeId, treeNode) {
                    if (treeNode.questionIds!=null){
                        return {color:"#ed5666", "font-weight":"normal"};
                    }
                    if (typeof treeNode.highlight == 'undefined' || treeNode.highlight == false) {
                        if(treeNode.materialId==0&&treeNode.nodeType==2){
                            return {color:"#dda525", "font-weight":"normal"};
                        }else{
                            return {color:"#333", "font-weight":"normal"};
                        }
                    } else {
                        return {color: "#A60000", "font-weight": "bold"};
                    }
                },
                showLine: true,
                selectedMulti: false
            },
            callback: {
                onAsyncError: function (event, treeId, treeNode, request, status) {//异步加载数据失败
                    icModale(0, "系统错误，章节获取失败");
                },
                onAsyncSuccess: function (event, treeId, treeNode, msg) {//异步加载数据成功

                    var treeObj = $.fn.zTree.getZTreeObj(treeId);
                    showHide(treeObj);
                }
            }
        };
        //初始化权限树

    //初始化权限树
    $.fn.zTree.init($('#'+id), settings);
}

/**
 * 显示和隐藏数据
 * @param treeObj
 */
function showHide(treeObj) {
    if ((treeObj).getNodes().length != 0) {
        $('#'+treeObj.setting.treeId).removeClass("hide");
        $('#'+treeObj.setting.treeId).next('div.noDataTipsBox').addClass('hide');
        treeObj.expandAll(true);
    } else {
        $('#'+treeObj.setting.treeId).addClass("hide");
        $('#'+treeObj.setting.treeId).next('div.noDataTipsBox').removeClass('hide');
    }
}

